package com.Maykk.Demo02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

/**
 * @ClassName: Demo01
 * @Description: TODO 模拟斗地主发牌
 * @Author: MaykkStar@163.com
 */
public class Demo01 {
    public static void main(String[] args) {
        // 创建 HashMpa 集合，存储所有牌
        HashMap<Integer, String> card = new HashMap<>();
        // 创建 Arraylist 集合，存储牌的 Key 值
        ArrayList<Integer> cardList = new ArrayList<Integer>();
        // 创建数组存放牌面花色和点数
        String[] colors = {"♥", "♠", "♦", "♣"};
        String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        // 把所有牌放进集合内
        int index = 0;
        for (String color : colors) {
            for (String num : nums) {
                card.put(index, color + num);
                cardList.add(index++);
            }
        } // 普通牌放入牌盒
        // 大小王放入
        card.put(index, "大");
        cardList.add(index++);
        card.put(index, "小");
        cardList.add(index);

        // 洗牌
        Collections.shuffle(cardList);

        // 定义玩家
        TreeSet<Integer> player01 = new TreeSet<>();
        TreeSet<Integer> player02 = new TreeSet<>();
        TreeSet<Integer> player03 = new TreeSet<>();
        TreeSet<Integer> lastCard = new TreeSet<>();

        // 发牌
        for (int i = 0; i < cardList.size(); i++) {
            if (i >= cardList.size() - 3) {
                lastCard.add(cardList.get(i));
            } else {
                switch (i % 3) {
                    case 0:
                        player01.add(cardList.get(i));
                        break;
                    case 1:
                        player02.add(cardList.get(i));
                        break;
                    case 2:
                        player03.add(cardList.get(i));
                        break;
                }
            }
        }
        // 看牌
        showCard("玩家1", player01, card);
        showCard("玩家2", player02, card);
        showCard("玩家3", player03, card);
        showCard("底牌", lastCard, card);
        System.out.println(card);
    }

    private static void showCard(String name, TreeSet<Integer> keys, HashMap<Integer, String> cards) {
        System.out.print(name + " : ");
        for (Integer key : keys) {
            System.out.print(cards.get(key) + "  ");
        }
        System.out.println("\n======================");
    }
}
